package com.oocl.heather.validator.impl;

import java.util.regex.Pattern;

import com.oocl.heather.validator.IValidator;

public class Validator implements IValidator {
//验证器
	public boolean validator(String item) {
		boolean flag=false;
		String id=item.substring(item.indexOf("id")+2, item.indexOf("name"));
//		String name=item.substring(item.indexOf("name")+1,item.indexOf("birth"));
		String birth=item.substring(item.indexOf("birth")+5, item.indexOf("sex"));
		String sex=item.substring(item.indexOf("sex")+3, item.indexOf("email"));
		String email=item.substring(item.indexOf("email")+5);
		
		String regId="[0-9]*";
		String regBirth = "^((\\d{2}(([02468][048])|([13579][26]))[-\\s]?((((0?[13578])|(1[02]))[-\\s]?((0?[1-9])"
				+ "|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[-\\s]?((0?[1-9])"
				+ "|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[-\\s]?((((0?[13578])|(1[02]))[-\\s]"
				+ "?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[-\\s"
				+ "]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
		String regSex="M|F";
		String regEmail="[a-zA-Z0-9_\\-\\.]+@[a-zA-Z0-9]+(\\.(com|cn|org|edu|hk))";
		
		boolean bid=isMatch(id, regId);
		boolean bbirth=isMatch(birth,regBirth);
		boolean bsex=isMatch(sex,regSex);
		boolean bemail=isMatch(email,regEmail);
		if(bid&&bbirth&&bsex&&bemail){
			flag=true;
		}
		
		return flag;
	}
	public static boolean isMatch(String str,String reg){ 
  	  Pattern pattern = Pattern.compile(reg); 
  	  return pattern.matcher(str).matches();  
  	} 
	
	
}
